﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.bear
{
    public class ProcurementAppService : ApplicationService, IProcurementAppService
    {
        private readonly IRepository<Procurement, int> _ProcurementRepository;
        private readonly IRepository<MaterialType, int> _materialTypes;
        private readonly LogisticsDbContext _logisticsDbContext;

        public ProcurementAppService(IRepository<Procurement, int> ProcurementRepository, IRepository<MaterialType, int> materialTypes, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _ProcurementRepository = ProcurementRepository;
            _materialTypes = materialTypes;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<ProcurementDto> CreateAsync(ProcurementDto Procurement)
        {
            var procurement = new Procurement
            {
                Name = Procurement.Name,
                ProcurementTypeId = Procurement.ProcurementTypeId,
                Material = Procurement.Material,
                Specifications = Procurement.Specifications,
                Origin = Procurement.Origin,
                Num = Procurement.Num,
                Deliver = DateTime.Now,
                Describe = Procurement.Describe,
                Applicant = Procurement.Applicant,
                Remarks = Procurement.Remarks,
                State = Procurement.State,
                UserId = Procurement.UserId,
                CreationTime = DateTime.Now,
            };

            await _ProcurementRepository.InsertAsync(procurement);
            return ObjectMapper.Map<Procurement, ProcurementDto>(procurement);
        }

        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _ProcurementRepository.DeleteAsync(id);
        }

        public async Task<ProcurementDto> GetAsync(int id)
        {
            var food = await _ProcurementRepository.GetAsync(id);
            return ObjectMapper.Map<Procurement, ProcurementDto>(food);
        }

        public async Task<PagedResultDto<ProcurementDto>> GetListAsync(GetProcurementListDto dto)
        {
            //var all = _ProcurementRepository.ToList();
            var all = _logisticsDbContext.Procurement.ToList();
            var type = _logisticsDbContext.MaterialType.ToList();
            var procType = from a in all
                           join c in type on a.ProcurementTypeId equals c.Id
                           select new Procurement
                           {
                               Applicant = a.Applicant,
                               CreationTime = a.CreationTime,
                               Deliver = a.Deliver,
                               Describe = a.Describe,
                               Material = a.Material,
                               Name = a.Name,
                               Num = a.Num,
                               Origin = a.Origin,
                               ProcurementTypeId = a.ProcurementTypeId,
                               Remarks = a.Remarks,
                               Specifications = a.Specifications,
                               State = a.State,
                               TypeName = c.Name,
                               UserId = a.UserId,
                               Id = a.Id
                           };
            int totoCount = procType.Count();
            var data = new PagedResultDto<ProcurementDto>();
            data.TotalCount = totoCount;
            procType = procType.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<Procurement>, List<ProcurementDto>>(procType.ToList());
            return data;
        }

        public async Task<List<GetMaterialTypeDto>> GetMaterialAsync()
        {
            var list = _materialTypes.ToList();
            return ObjectMapper.Map<List<MaterialType>, List<GetMaterialTypeDto>>(list);
        }

        public int UpdateAsync(ProcurementDto Procurement)
        {
            var food = new Procurement
            {
                Id=Procurement.Id,
                Name = Procurement.Name,
                Applicant = Procurement.Applicant,
                CreationTime = DateTime.Now,
                Deliver = DateTime.Now,
                Describe = Procurement.Describe,
                Material = Procurement.Material,
                ProcurementTypeId = Procurement.ProcurementTypeId,
                Specifications = Procurement.Specifications,
                Origin = Procurement.Origin,
                Num = Procurement.Num,
                Remarks = Procurement.Remarks,
                State = Procurement.State,
                UserId = Procurement.UserId,
            };
            _logisticsDbContext.Entry(food).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
